package com.datang.petstore.service.impl;


import com.datang.petstore.entity.MicroblogComment;
import com.datang.petstore.entity.MicroblogCommentLikeRecord;
import com.datang.petstore.entity.MicroblogCommentLikeRecordExample;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.generatemapper.MicroblogCommentLikeRecordMapper;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogCommentLikeRecordService;
import com.datang.petstore.service.MicroblogCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-11-07 16:10
 * @notify 微博评论点赞接口
 * @version 1.0
 */


@Service
public class MicroblogCommentLikeRecordServiceImpl implements MicroblogCommentLikeRecordService {

    @Autowired
    private MicroblogCommentLikeRecordMapper microblogCommentLikeRecordMapper;

    @Autowired
    private MicroblogCommentService microblogCommentService;

    /*
     * @author 顶风少年
     * @date 2019/11/7 16:25
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogCommentId
     * @param userId
     * @result java.util.List<com.datang.petstore.entity.MicroblogCommentLikeRecord>
     * @notify 获取用户点赞
     * version 1.0
     */
    public List<MicroblogCommentLikeRecord> getMicroblogCommentLikeRecord(Integer microblogCommentId,String userId){
        MicroblogCommentLikeRecordExample microblogCommentLikeRecordExample = new MicroblogCommentLikeRecordExample();
        MicroblogCommentLikeRecordExample.Criteria criteria = microblogCommentLikeRecordExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andMicroblogCommentIdEqualTo(microblogCommentId);
        List<MicroblogCommentLikeRecord> microblogCommentLikeRecords = microblogCommentLikeRecordMapper.selectByExample(microblogCommentLikeRecordExample);
        return microblogCommentLikeRecords;
    }

    /*
     * @author 顶风少年
     * @date 2019/11/7 16:27
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblogCommentLikeRecord
     * @result void
     * @notify 给评论点赞
     * version 1.0
     */
    @Transactional
    public void addMicroblogCommentLikeRecord(MicroblogCommentLikeRecord microblogCommentLikeRecord){

        //获取评论是否存在
        MicroblogComment microblogComment = microblogCommentService.getMicroblogComment(microblogCommentLikeRecord.getMicroblogCommentId());

        List<MicroblogCommentLikeRecord> microblogCommentLikeRecord1 = getMicroblogCommentLikeRecord(microblogCommentLikeRecord.getMicroblogCommentId(), microblogCommentLikeRecord.getUserId());
        if (microblogCommentLikeRecord1.size()==1){
            throw new AcceptResultCodeException(ResultCode.error510);
        }

        //补充数据
        microblogCommentLikeRecord.setDate(new Date());
        microblogCommentLikeRecord.setMicroblogCommentUserId( microblogComment.getUserId());
        microblogCommentLikeRecordMapper.insertSelective(microblogCommentLikeRecord);

    }
}
